<template>
  <div class="container">
    <div class="app-container">
      <div class="card-container">
        <!-- 搜索区域 -->
        <div class="search-container">
          <span class="search-label">车牌号码：</span>
          <el-input v-model="params.carNumber" clearable placeholder="请输入内容" class="search-main" />
          <span class="search-label">车主姓名：</span>
          <el-input v-model="params.personName" clearable placeholder="请输入内容" class="search-main" />
          <span class="search-label">状态：</span>
          <el-select v-model="params.cardStatus">
            <el-option
              v-for="item in cardStatusList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
          <el-button type="primary" class="search-btn" @click="onSearch">查询</el-button>
        </div>
        <!-- 新增删除操作区域 -->
        <div class="create-container">
          <el-button type="primary" @click="$router.push('/cardAdd')">添加月卡</el-button>
          <el-button>批量删除</el-button>
        </div>
        <!-- 表格区域 -->
        <div class="table">
          <el-table style="width: 100%" :data="cardList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column type="index" label="序号" />
            <el-table-column prop="personName" label="车主名称" />
            <el-table-column prop="phoneNumber" label="联系方式" />
            <el-table-column prop="carNumber" label="车牌号码" />
            <el-table-column prop="carBrand" label="车辆品牌" />
            <el-table-column prop="totalEffectiveDate" label="剩余有效天数" />
            <el-table-column label="状态" prop="cardStatus" :formatter="formatStatus" />
            <el-table-column label="操作" fixed="right" width="180">
              <template #default="scope">
                <el-button size="mini" type="text">续费</el-button>
                <el-button size="mini" type="text">查看</el-button>
                <el-button size="mini" type="text" @click="editCard(scope.row.id)">编辑</el-button>
                <el-button size="mini" type="text" @click="delCartList">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 分页 -->
        <div class="page-container">
          <!--  @current-change="pageChange" 监听当前点击了第几页 -->
          <el-pagination
            layout="total, prev, pager, next,sizes"
            :page-sizes="[1,2,3,10]"
            :total="total"
            :page-size="params.pageSize"
            @current-change="pageChange"
            @size-change="sizeChangeFn"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCardListApi, getDelAllCardApi } from '@/api/car'

export default {
  data() {
    return {
      params: {
        page: 1, // 页数
        pageSize: 5, // 条数
        carNumber: null, // 车辆号码
        personName: null, // 车主姓名
        cardStatus: null // 车辆状态
      },
      cardList: [],
      total: 0, // 总条数
      // 月卡状态
      cardStatusList: [
        { id: null, name: '全部' },
        { id: 0, name: '可用' },
        { id: 1, name: '已过期' }
      ],
      // 已选择列表
      selectedCarList: []
    }
  },
  created() {
    this.getCardList()
  },
  methods: {
    async getCardList() {
      const { rows, total } = await getCardListApi(this.params)
      // console.log(res)
      this.cardList = rows
      this.total = total
    },
    formatStatus(row) {
      const Map = {
        0: '可用',
        1: '已过期'
      }
      return Map[row.cardStatus]
    },
    pageChange(page) {
      // 把点击的页数赋值给请求参数页数
      this.params.page = page
      // 使用最新的请求参数获取列表数据
      this.getCardList()
    },
    sizeChangeFn(pageSize) {
      // console.log(pageSize)
      this.params.pageSize = pageSize
      this.getCardList()
    },
    onSearch() {
      // 调用接口之前把页数参数重置为 1 （防止页码在其他页时，搜索时数据不够，导致还在原来页码停留）
      this.params.page = 1
      this.getCardList()
    },
    editCard(id) {
      this.$router.push({
        path: '/cardAdd',
        query: {
          id
        }
      })
    },
    handleSelectionChange(rowList) {
      console.log(rowList)
      this.selectedCarList = rowList
    },
    delCartList() {
      this.$confirm('此操作将永久删除选择的月卡, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
        // 未选择删除的 id，给提示
          if (this.selectedCarList.length === 0) {
            this.$message({
              type: 'warning',
              message: '请选择要删除的月卡!'
            })
          } else {
          // 处理id
            await getDelAllCardApi(this.selectedCarList.map(item => item.id))
            this.getCardList()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        }).catch((error) => {
          console.log(error)
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}
.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
.table {
  background: #f4f6f8;
}
</style>

